package cn.tedu.yxcar.product.controller;

import cn.tedu.yxcar.product.pojo.vo.CarListVO;
import cn.tedu.yxcar.product.pojo.vo.CarShowVO;
import cn.tedu.yxcar.product.pojo.vo.DetailListVO;
import cn.tedu.yxcar.product.pojo.vo.DetailShowVO;
import cn.tedu.yxcar.product.service.DetailService;
import cn.tedu.yxcar.product.web.JsonPage;
import cn.tedu.yxcar.product.web.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/detail")
@Api(tags = "汽车购买模块")
@Validated
public class DetailController {

    @Autowired
    private DetailService detailService;

    @GetMapping("/select")
    @ApiOperation("搜索汽车列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",example = "10"),
            @ApiImplicitParam(value = "关键词",name = "keywords",example = "20")
    })
    public JsonResult<JsonPage<DetailListVO>> listSeckillSpus(
            @Param("page") Integer page, @Param("pageSize") Integer pageSize, @Param("keywords") String keywords){
        PageHelper.startPage(page,pageSize);
        List<DetailListVO> list=
                detailService.getCarByName(keywords);
        JsonPage<DetailListVO> jsonPage = JsonPage.restPage(PageInfo.of(list));
        return JsonResult.ok(jsonPage);
    }


    @GetMapping("/getCarByName")
    @ApiOperation("根据关键词展示汽车列表")
    @ApiImplicitParam(value = "关键词",name = "key",example = "1")
    public JsonResult getCarByName(
            @Param("key") String key){
        List<DetailListVO> list = detailService.getCarByName(key);
        System.out.println(list);
        return JsonResult.ok(list);
    }

    @GetMapping("/selectByPrice")
    @ApiOperation("根据车价查找")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",example = "10"),
            @ApiImplicitParam(value = "价格1",name = "price1",example = "20"),
            @ApiImplicitParam(value = "价格2",name = "price2",example = "20")
    })
    public JsonResult<JsonPage<DetailListVO>> selectByPrice(
            @Param("page") Integer page, @Param("pageSize") Integer pageSize, @Param("price1") Integer price1, @Param("price2") Integer price2){
        PageHelper.startPage(page,pageSize);
        List<DetailListVO> list=
                detailService.selectByPrice(price1,price2);
        JsonPage<DetailListVO> jsonPage = JsonPage.restPage(PageInfo.of(list));
        return JsonResult.ok(jsonPage);
    }

    @GetMapping("/searPriceBig")
    @ApiOperation("价格最高")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",example = "10"),
    })
    public JsonResult<JsonPage<DetailListVO>> searPriceBig(
            @Param("page") Integer page, @Param("pageSize") Integer pageSize){
        PageHelper.startPage(page,pageSize);
        List<DetailListVO> list=
                detailService.searPriceBig();
        JsonPage<DetailListVO> jsonPage = JsonPage.restPage(PageInfo.of(list));
        return JsonResult.ok(jsonPage);
    }

    @GetMapping("/searPriceSmall")
    @ApiOperation("价格最低")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",example = "10"),
    })
    public JsonResult<JsonPage<DetailListVO>> searPriceSmall(
            @Param("page") Integer page, @Param("pageSize") Integer pageSize){
        PageHelper.startPage(page,pageSize);
        List<DetailListVO> list=
                detailService.searPriceSmall();
        JsonPage<DetailListVO> jsonPage = JsonPage.restPage(PageInfo.of(list));
        return JsonResult.ok(jsonPage);
    }

    @GetMapping("/searTimeNew")
    @ApiOperation("最新发布")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",example = "10"),
    })
    public JsonResult<JsonPage<DetailListVO>> searTimeNew(
            @Param("page") Integer page, @Param("pageSize") Integer pageSize){
        PageHelper.startPage(page,pageSize);
        List<DetailListVO> list=
                detailService.searTimeNew();
        JsonPage<DetailListVO> jsonPage = JsonPage.restPage(PageInfo.of(list));
        return JsonResult.ok(jsonPage);
    }


    @GetMapping("/searMileageSmall")
    @ApiOperation("里程最短")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",example = "10"),
    })
    public JsonResult<JsonPage<DetailListVO>> searMileageSmall(
            @Param("page") Integer page, @Param("pageSize") Integer pageSize){
        PageHelper.startPage(page,pageSize);
        List<DetailListVO> list=
                detailService.searMileageSmall();
        JsonPage<DetailListVO> jsonPage = JsonPage.restPage(PageInfo.of(list));
        return JsonResult.ok(jsonPage);
    }

    @GetMapping("/searOldSmall")
    @ApiOperation("车龄最短")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",example = "10"),
    })
    public JsonResult<JsonPage<DetailListVO>> searOldSmall(
            @Param("page") Integer page, @Param("pageSize") Integer pageSize){
        PageHelper.startPage(page,pageSize);
        List<DetailListVO> list=
                detailService.searOldSmall();
        JsonPage<DetailListVO> jsonPage = JsonPage.restPage(PageInfo.of(list));
        return JsonResult.ok(jsonPage);
    }

    @GetMapping("/searTypes")
    @ApiOperation("根据更多条件查找")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",example = "10"),
            @ApiImplicitParam(value = "车型",name = "vehicle",example = "20"),
            @ApiImplicitParam(value = "车龄",name = "carage",example = "20"),
            @ApiImplicitParam(value = "变速箱",name = "carcase",example = "20")
    })
    public JsonResult<JsonPage<DetailListVO>> searTypes(
            @Param("page") Integer page, @Param("pageSize") Integer pageSize, @Param("vehicle") String vehicle, @Param("carage") String carage,@Param("carcase") String carcase){
        PageHelper.startPage(page,pageSize);
        if (carage==null){
            carage="100年以内";
        }
        Integer caragenum=(int)carage.charAt(0);
        List<DetailListVO> list=
                detailService.searTypes(vehicle,caragenum,carcase);
        JsonPage<DetailListVO> jsonPage = JsonPage.restPage(PageInfo.of(list));
        return JsonResult.ok(jsonPage);
    }



}
